package com.mantru.iotpot.Begin;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Util.ToastUtils;
import com.mantru.iotpot.eventbus.EventMessage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import com.mantru.iotpot.Tools.tools;


public class LoginActivity extends AppCompatActivity{
    private ApplicationClass ac;
    private EditText editText1;
    private EditText editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        editText1 = findViewById(R.id.EdText1);
        editText2 = findViewById(R.id.EdText2);
        editText1.setText("cen001");
        editText2.setText("123456");
        setEdittextListener();
        findViewById(R.id.button1).setOnClickListener(v -> {
            check();
            tools.hideKeyboard(LoginActivity.this);
        });
    }

    private void setEdittextListener(){
        editText1.setOnEditorActionListener((v, actionId, event) -> {
            // 检查是否是按下 Enter 键或者完成操作
            if ((event != null && (event.getAction() == KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_SEND) {
                // 获取输入法管理器
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                // 隐藏软键盘
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return true;
            }
            return false;
        });

        editText2.setOnEditorActionListener((v, actionId, event) -> {
            // 检查是否是按下 Enter 键或者完成操作
            if ((event != null && (event.getAction() == KeyEvent.ACTION_DOWN) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_SEND) {
                // 获取输入法管理器
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                // 隐藏软键盘
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                return true;
            }
            return false;
        });
    }

    public void check(){
        String account = editText1.getText().toString();
        String password = editText2.getText().toString();
        if (account.isEmpty()){
            //Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            ToastUtils.show(this,"请输入账号");
        } else if (password.isEmpty()) {
            //Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            ToastUtils.show(this,"请输入密码");
        }else {
            ac.APP_Login(account, password);
        }
    }
    // 在Activity可见时注册EventBus
    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    //eventbus解注册
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    // eventbus解注册
    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(EventMessage event){
        if (event.id == EventMessage.MSG_ID_LoginResult){
            Log.d("LoginActivity","event.message: " + event.message);
            if (event.message.contains("true")){
                startActivity(new Intent(this, BeginSetActivity.class));
            }else {
                //Toast.makeText(this,"账号密码错误",Toast.LENGTH_SHORT).show();
                ToastUtils.show(this,"账号密码错误");
            }
        }
    }
}